// pages/reportdate/reportdate.js
const chineseNumbers=['一', '二', '三', '四', '五', '六', '七', '八', '九','十','十一','十二'];
const MIN_YEAR=2000;
const MAX_YEAR=new Date().getFullYear();
function dateFormat(date) {
    const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 获取月份并补齐两位
    const day = date.getDate().toString().padStart(2, '0'); // 获取日期并补齐两位
    return `${month}.${day}`; // 组合为 MM.DD 格式
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        MIN_YEAR:MIN_YEAR,
        MAX_YEAR:MAX_YEAR,
        MIN_TIME:new Date(MIN_YEAR,0,1).getTime(),
        MAX_TIME:new Date(MAX_YEAR,0,1).getTime(),

        year: 2024,
        years:[],

        months: [],
        currentYear:new Date().getFullYear(),
        currentMonth: new Date().getMonth()+1,

        pickerVisible:false
        

    },
    getWeeks(year) {
        var months = [];
        var beforeToday = true;
        var today = new Date();
        today.setHours(0, 0, 1, 0);
        for (var month = 1; month <= 12; month++) {
            let date = new Date(year, month - 1, 1);
            var weeks = [];

            // 找到第一个周一
            while (date.getDay() !== 1) {
                date.setDate(date.getDate() + 1);
            }
            // 遍历每个周一，并将其作为一周的开始日期
            var weekIndex=0;
            while (date.getMonth() === month - 1) {
                var startOfWeek = new Date(date);

                // 获取周日（周一 + 6 天）
                var endOfWeek = new Date(date);
                endOfWeek.setDate(endOfWeek.getDate() + 6);

                beforeToday = endOfWeek.getTime() < today.getTime()

                // 保存每周的范围
                weeks.unshift({
                    year: year.toString()+'年',
                    month: (month ).toString()+'月',
                    startDate: dateFormat(startOfWeek), // 格式化为 YYYY-MM-DD
                    startTime: Math.floor(startOfWeek.getTime() / 1000),
                    endDate: dateFormat(endOfWeek),
                    endTime: Math.floor(endOfWeek.getTime() / 1000),
                    note: beforeToday ? dateFormat(startOfWeek) + '-' + dateFormat(endOfWeek) : '将在下一周出炉，敬请期待',
                    index: chineseNumbers[weekIndex],
                    clickable:beforeToday
                });
                if (!beforeToday)
                    break;

                // 移动到下一个周一
                date.setDate(date.getDate() + 7);
                weekIndex+=1;
            }
            months.unshift(weeks)
            if (!beforeToday)
                break;

        }
        this.setData({
            year:year,
            months: months,
            monthsLength:months.length
        })
    },
    toReport(e){
        console.log(e);
        wx.navigateTo({
          url: '../report/report?data='+JSON.stringify(e.currentTarget.dataset.data),
        })
    },
    goLastYear(e){
        if(this.data.year>this.data.MIN_YEAR){
            this.setData({
                year:this.data.year-1
            })
        }
        this.getWeeks(this.data.year)
    },
    goNextYear(e){
        if(this.data.year<this.data.MAX_YEAR){
            this.setData({
                year:this.data.year+1
            })
        }
        this.getWeeks(this.data.year)
    },
    yearClick(e){
        this.setData({
            pickerVisible:true
        })
    },
    onPickerConfirm(e){
        console.log(e)
        this.setData({
            year:e.detail.value
        })
        this.getWeeks(this.data.year)
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getWeeks(new Date().getFullYear())
        var years=[];
        for(var i=this.data.MIN_YEAR;i<=this.data.MAX_YEAR;i++)
        {
            years.push({
                label:i.toString()+'年',
                value:i
            });
        }
        this.setData({
            years:years
        })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})